package com.link.base.base.message.service;

import com.link.base.base.message.dao.mybatis.mapper.MessageMapper;
import com.link.base.base.message.model.*;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息管理 - 移动/WEB端数据展示接入
 *
 * @author ouyangzhenqiang
 * @date 2018/10/26
 */
@Service
public class MessageServiceImpl extends BasicServiceImpl<Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    @Override
    public BasicMapper<Message> getBasicMapper() throws Exception {
        return messageMapper;
    }

    @Override
    public void readAllUpdate(Message entity) {
        if (null == entity.getUserId()) {
            throw new BasicServiceException("userId不能为空");
        }
        // 消息已读的标记
        String readFlag = "Y";
        entity.setIsRead(readFlag);
        messageMapper.readAllUpdate(entity);

    }

    @Override
    public List<MessageModuleNum> queryModuleUnReadCount(MessageModuleNum record) throws Exception {
        List<MessageModuleNum> moduleNumList = messageMapper.queryModuleUnReadCount(record);
        // 合并已读/未读消息记录总数
        List<MessageModuleNum> tempList = new ArrayList<MessageModuleNum>();
        for (MessageModuleNum item : moduleNumList) {
            String module = item.getModule();
            if (tempList.size() == 0) {
                if ("Y".equals(item.getIsRead())) {
                    item.setReadNum(item.getNum());
                } else {
                    item.setUnReadNum(item.getNum());
                }
                tempList.add(item);
            } else {
                boolean find = false;
                for (MessageModuleNum tempItem : tempList) {
                    if (module.equals(tempItem.getModule())) {
                        if ("Y".equals(item.getIsRead())) {
                            tempItem.setReadNum(item.getNum());
                        } else {
                            tempItem.setUnReadNum(item.getNum());
                        }
                        find = true;
                        break;
                    }
                }
                if (!find) {
                    if ("Y".equals(item.getIsRead())) {
                        item.setReadNum(item.getNum());
                    } else {
                        item.setUnReadNum(item.getNum());
                    }
                    tempList.add(item);
                }
            }
        }
        // 查询各模块下最新一条消息记录的内容、创建时间、提醒时间
        if (StringUtils.isNotBlank(record.getNeedNoteFlag())) {
            if (tempList.size() > 0) {
                for (MessageModuleNum item : tempList) {
                    MessageModuleNum moduleItem = messageMapper.getLastMessageByModule(item);
                    if (moduleItem != null) {
                        item.setNote(moduleItem.getNote());
                        item.setCreatedTime(moduleItem.getCreatedTime());
                        item.setRemindTime(moduleItem.getRemindTime());
                    }
                }
            }
        }
        return tempList;
    }

    @Override
    public MessageModuleNum getLastMessageByModule(MessageModuleNum record) throws Exception {
        return messageMapper.getLastMessageByModule(record);
    }

    @Override
    public void setModuleReadedUpdate(Message record) throws Exception {
        messageMapper.setModuleReadedUpdate(record);
    }

    @Override
    public void setReadedById(Message record) throws Exception {
        messageMapper.setReadedById(record);
    }

    @Override
    public int countByModule(MessageModuleNum record) throws Exception {
        return messageMapper.countByModule(record);
    }

    @Override
    public List<Message> queryExits(Message record) throws Exception {
        return messageMapper.queryExits(record);
    }

    @Override
    public void deleteByObjectId(Message record) throws Exception {
        messageMapper.deleteByObjectId(record);
    }

    @Override
    public List<MessageTask> queryTaskByExamplePage(MessageModuleNum record) throws Exception {
        return messageMapper.queryTaskByExamplePage(record);
    }

    @Override
    public List<MessageOrder> queryOrderByExamplePage(MessageModuleNum record) throws Exception {
        List<MessageOrder> list = messageMapper.queryOrderByExamplePage(record);
        //查询订单产品列表
        for (MessageOrder l : list) {
            MessageOrderProduct item = new MessageOrderProduct();
            item.setOrderId(l.getOrderId());
            List<MessageOrderProduct> prodList = messageMapper.queryMessageOrderMeProductList(item);
            l.setProdList(prodList);
        }
        return list;
    }

    @Override
    public List<Message> queryReportByExamplePage(MessageModuleNum record) throws Exception {
        return messageMapper.queryReportByExamplePage(record);
    }

    @Override
    public List<MessageNews> queryNewsByExamplePage(MessageModuleNum record) throws Exception {
        return messageMapper.queryNewsByExamplePage(record);
    }

    @Override
    public List<MessageComment> queryCommentByExamplePage(MessageModuleNum record) throws Exception {
        return messageMapper.queryCommentByExamplePage(record);
    }

    @Override
    public List<MessageComment> queryNoticeByExamplePage(MessageModuleNum record) throws Exception {
        return messageMapper.queryNoticeByExamplePage(record);
    }

    @Override
    public List<Message> queryPushByExamplePage(MessageModuleNum record) throws Exception {
        return messageMapper.queryPushByExamplePage(record);
    }

    @Override
    public List<Message> getUnDealMessageByObjectId(Message record) throws Exception {
        return messageMapper.getUnDealMessageByObjectId(record);
    }

    @Override
    public void delMessageById(Message record) throws Exception {
        messageMapper.delMessageById(record);
    }
}
